#include <iostream>
#include <fcntl.h>
#include <unistd.h>

void SetNonBlocking(int fd)
{
    int fl = fcntl(fd, F_GETFL);
    if (fl < 0)
    {
        perror("fcntl fail");
        exit(1);
    }

    if (fcntl(fd, F_SETFL, fd | O_NONBLOCK) < 0)
    {
        perror("fcntl fail");
        exit(1);
    }
}

int main()
{
    char buffer[1024];
    SetNonBlocking(0);
    while(1)
    {
        int n = read(0 , buffer , sizeof(buffer));
        if(n > 0)
        {
            std::cout << " get a message : " << buffer <<  "\n";
        }
        else if(n == 0)
        {
            break;
        }
        else 
        {
            if(errno == EAGAIN)
            {
                std::cout << " I am waiting for you" << "\n";
                continue;
            }
            else 
            {
                perror("read fail : ");
                exit(2);
            }
        }
    }
    return 0;
}
